package sapling.config;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by wuzh on 2016/12/27.
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    private final static String UNAUTHORIZED_URL = "admin/403";//todo:改为跳转
    private final static String ERROR_URL = "redirect:/error";

    @ExceptionHandler(value = AuthorizationException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public ModelAndView unauthorizedException(HttpServletRequest req, Exception e) throws Exception {
        ModelAndView mav = new ModelAndView();
        mav.setViewName(UNAUTHORIZED_URL);
        mav.addObject("exception", e);
        return mav;
    }

    @ExceptionHandler(value = Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
        ModelAndView mav = new ModelAndView();
        mav.addObject("exception", e);
        mav.setViewName(ERROR_URL);
        return mav;
    }
}
